<template>
  <div class="search-result">
    <!-- 头部 -->
    <van-nav-bar :title="keyword + '的结果'"
                 left-arrow
                 @click-left="$router.back()"
                 fixed></van-nav-bar>
    <!-- 结果 -->
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="item in list"
                :key="item.art_id.toString()"
                :title="item.title"
                @click="$router.push('/article/' + item.art_id.toString())" />
    </van-list>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'SearchResult',
  computed: {
    keyword() {
      return this.$route.query.keyword
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad() {
      const res = await getSearch(this.keyword, this.page)
      this.list = [...this.list, ...res.data.results]
      // 加载完成了, 把loading改成false
      this.loading = false
      // 页面加1
      this.page++
      if (res.data.results === 0 || this.list.length >= 50) {
        // finished为true显示没有更多了
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result {
  padding-top: 46px;
}
</style>
